package com.sunday.common.core.remoting.base.page;

import com.sunday.common.core.lambda.LambdaOperation;
import com.sunday.common.core.lambda.toolkit.support.SFunction;
import com.sunday.common.core.utils.ReflectToJsonUtils;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 排序元素载体 DTO
 *
 * @author zsy
 * @since 2022/10/14
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class OrderItemDTO implements Serializable {

    /**
     * 需要进行排序的字段
     */
    @Schema(description = "需要进行排序的字段")
    private String column;
    /**
     * 是否正序排列
     */
    @Schema(description = "是否正序排列")
    private boolean asc;

    public static OrderItemDTO of(String column, boolean asc) {
        return new OrderItemDTO(column, asc);
    }

    public static <T> OrderItemDTO of(SFunction<T, ?> column, boolean asc) {
        return new OrderItemDTO(LambdaOperation.getFieldName(column), asc);
    }

    @Override
    public String toString() {
        return ReflectToJsonUtils.toJson(this);
    }
}
